package com.jiyun.behavior.service.ipl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiyun.behavior.mapper.ApBehaviorEntryMapper;
import com.jiyun.behavior.mapper.ApUnlikesBehaviorMapper;
import com.jiyun.behavior.service.ApUnlikesBehaviorSerivce;
import com.usian.model.behavior.ApBehaviorEntry;
import com.usian.model.behavior.ApUnlikesBehavior;
import com.usian.model.behavior.UnLikesBehaviorDto;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class ApUnlikesBehaviorSerivceipl  extends ServiceImpl<ApUnlikesBehaviorMapper,ApUnlikesBehavior> implements ApUnlikesBehaviorSerivce {

    @Resource
    ApBehaviorEntryMapper apBehaviorEntryMapper;

    @Resource
    ApUnlikesBehaviorMapper apUnlikesBehaviorMapper;

    @Override
    public ResponseResult buxihuanxingwei(UnLikesBehaviorDto dto) {
        if (dto==null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        LambdaQueryWrapper<ApBehaviorEntry> qw=new LambdaQueryWrapper<>();
        qw.eq(ApBehaviorEntry::getEntryId,dto.getEquipmentId());
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryMapper.selectOne(qw);
        if (apBehaviorEntry==null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        LambdaQueryWrapper<ApUnlikesBehavior> qw2=new LambdaQueryWrapper<>();
        qw2.eq(ApUnlikesBehavior::getArticleId,dto.getArticleId());
        qw2.eq(ApUnlikesBehavior::getEntryId,apBehaviorEntry.getId());
        ApUnlikesBehavior apUnlikesBehavior = apUnlikesBehaviorMapper.selectOne(qw2);
        if (apUnlikesBehavior==null){
            if (dto.getType()==0){
                LambdaUpdateWrapper<ApUnlikesBehavior> qwupdate=new LambdaUpdateWrapper<>();
                apUnlikesBehavior.setType(0);
                apUnlikesBehaviorMapper.update(apUnlikesBehavior,qwupdate);
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
            }else {
                LambdaUpdateWrapper<ApUnlikesBehavior> qwupdate=new LambdaUpdateWrapper<>();
                apUnlikesBehavior.setType(1);
                apUnlikesBehaviorMapper.update(apUnlikesBehavior,qwupdate);
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
            }
        }
        ApUnlikesBehavior apUnlikesBehavior1=new ApUnlikesBehavior();
        apUnlikesBehavior1.setType(dto.getType());
        apUnlikesBehavior1.setArticleId(dto.getArticleId());
        apUnlikesBehavior1.setCreatedTime(new Date());
        apUnlikesBehavior1.setEntryId(dto.getEquipmentId());
        apUnlikesBehaviorMapper.insert(apUnlikesBehavior1);


        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
